package com.loongtech.bi.action.flow;


import com.loongtech.bi.action.Base2BiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

@Controller
@Scope("view")
public class FightVariationAction extends Base2BiSearchAction {
	private static final long serialVersionUID = 1L;
    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 3 * Time.DAY));
    // 查询bean
    private String userId;
    private String roleId;
    private List<Object[]> result;  // 结果List

    @Resource
    HadoopQueryManager hadoopQueryManager;

    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private Session session;

    @PostConstruct
    public void init() {
        result = new ArrayList<>();
    }

    @Override
    public void search() {
        sysLogManager.addLog(session.getUserName(), "查询流水查询—战斗力流水");
        FacesContext context = FacesContext.getCurrentInstance();

        // 开始结束时间不能大于当前时间60天
        if (Time.getInterval(new Date(), dateStart) > 60 || Time.getInterval(new Date(), dateEnd) > 60) {
            context.addMessage(null, new FacesMessage("提示", "开始结束时间不能小于当前时间60天!"));
            return;
        }
        if (Time.getInterval(dateStart, dateEnd) < 0) {
            context.addMessage(null, new FacesMessage("提示", "开始时间不能大于结束时间!"));
            return;
        }

        // 用户ID，角色ID必要有一个非空
        if (StringUtils.isBlank(userId) && StringUtils.isBlank(roleId)) {
            context.addMessage(null, new FacesMessage("提示", "用户ID，角色ID必须至少有一个非空!"));
            return;
        }

        if (HadoopQueryManager.VISIT_QUEUE.offer("FightVariationAction")) {
            System.out.println("VISIT_QUEUE offer FightVariationAction ok!");

            StringBuilder sql = new StringBuilder();
            sql.append("select logtime,platform,channel,serverid,userid,roleid,rolelevel,gangid,fightvalue,viplevel,mac,logid,loginip from userheartbeat where 1=1 ");
            if (!StringUtils.isBlank(userId)) {
                sql.append(" and userid = '");
                sql.append(userId.trim());
                sql.append("' ");
            }
            if (!StringUtils.isBlank(roleId)) {
                sql.append(" and cast(roleid as string) = '");
                sql.append(roleId.trim());
                sql.append("' ");
            }
            if (!"0".equals(queryPlatform)) {
                sql.append(" and platform = ");
                sql.append(queryPlatform);
            }
            if (!"0".equals(queryChannel)) {
                sql.append(" and channel = '");
                sql.append(queryChannel);
                sql.append("' ");
            }
            if (!"0".equals(queryServer)) {
                sql.append(" and serverid = ");
                sql.append(queryServer);
            }
            sql.append(" and dt >= '");
            sql.append(Time.getDateStr(dateStart));
            sql.append("' and dt <= '");
            sql.append(Time.getDateStr(dateEnd));
            sql.append("' ");
            sql.append(" order by logtime desc ");
            System.out.println(sql.toString());
            Entry<List<String>, List<Object[]>> ret = hadoopQueryManager.executeSQL(sql.toString());
            if (null == ret || null == ret.getKey() || ret.getKey().isEmpty() || null == ret.getValue() || ret.getValue().size() <= 0) {
                result.clear();
                context.addMessage(null, new FacesMessage("提示", "查询结果为空!"));
                return;
            }

            result = ret.getValue();
        } else {
            System.out.println("VISIT_QUEUE offer FightVariationAction failed!");
            context.addMessage(null, new FacesMessage("提示", "当前系统正被访问，为保证服务器稳定，请稍后再试"));
        }
    }

    @Override
    protected String csvContent() {
        return null;
    }

    @Override
    protected String csvFileName() {
        return null;
    }


    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public List<Object[]> getResult() {
        return result;
    }

    public void setResult(List<Object[]> result) {
        this.result = result;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Override
    public Date getDateStart() {
        return dateStart;
    }

    @Override
    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }
}
